import requests
import sqlite3,os,time
import requests
import win32crypt
s= requests.Session()
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.8.0.16453",
"Origin": "http://www.bch91.com",
"Referer": "http://www.bch91.com/s/game/",
"X-Requested-With": "XMLHttpRequest",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.8",
"Accept":"",
"Connection": "keep-alive",}

s.headers=headers
def get_code():
    while 1:
        url='http://buy.cqcp.net/Game/GetNum.aspx?iType=11&name=0.8628251877158023'
        cont=s.get(url).content
        if '180206034' in cont:
        #if 1:
            print cont
            url2='http://www.bch91.com/game/getNo/1'
            info=s.get(url2,cookies=COOKIES).json()
            print info
            return cont.split('|')[-1]
        else:
            print cont


def get_cookies(host='www.bch91.com'):
  #cookiepath=os.environ['LOCALAPPDATA']+r"\Google\Chrome\User Data\Default\Cookies"
  cookiepath=os.environ['LOCALAPPDATA']+r"\2345Explorer\User Data\Default\CookiesV3"
  sql="select host_key,name,encrypted_value from cookies where host_key='%s'" % host
  with sqlite3.connect(cookiepath) as conn:
    cu=conn.cursor()
    cookies={name:win32crypt.CryptUnprotectData(encrypted_value)[1].decode() for host_key,name,encrypted_value in cu.execute(sql).fetchall()}
    return cookies
COOKIES=get_cookies()
def post_code():
    url='http://www.bch91.com/s/'
    url2='http://www.bch91.com/game/getNo/1'
    info=s.get(url2,cookies=COOKIES).json()
    print info
    code=get_code()
    data={
        "code[0][fanDian]":"0.0",
"code[0][bonusProp]":"195600.0",
"code[0][mode]":"0.02",
"code[0][modeAmount]":"2",
"code[0][beiShu]":"1",
"code[0][orderId]": str(int(str(time.time()).replace('.','')+'0')-2147483647*623),
"code[0][actionData]":code,
"code[0][actionNum]":"1",
"code[0][weiShu]":"0",
"code[0][ct]":"1",
"code[0][g]":"1",
"code[0][p]":"2",
"para[type]":"1",
"para[actionNo]":info['actionNo'],
"para[kjTime]":info['actionTime'],
"para[uu]":info['touzhuuu'],
}
    url='http://www.bch91.com/game/postCode'
    print data
    print s.post(url,data=data,cookies=COOKIES).text
    
if __name__=='__main__':
    print post_code()
